/******************************
*                             *
*        Jared Szechy         *
*     CPS 444 Extra Credit    *
*                             *
*       Works with calc4      *
*                             *
******************************/

#include <stdio.h>
#include "calc.h"
#include "calc.tab.h"

static int lbl;

/* does a depth-first traversal; therefore visits nodes in the
   order that they were allocated during parsing */
int dfs(nodeType* p) {

   int lbl1, lbl2;

   if (!p) return 0;

   switch (p->type) {

      case typeCon:

         printf ("%d", p->con_or_id.value_or_i);
         break;

      case typeId:

         printf ("%c", p->con_or_id.value_or_i + 'a');
         break;

      case typeOpr:

         switch (p->opr.oper) {

            case WHILE:

               printf ("while(");
               dfs (p->opr.op[0]);
               printf (") {\n");
               
               dfs (p->opr.op[1]);
               printf ("}\n");
               break;

            case IF:

               printf("if (");
               dfs (p->opr.op[0]);
               printf(") {\n");
               dfs (p->opr.op[1]);
               printf("}\n");
               if (p->opr.nops > 2) {

                  /* if else */
                  printf("else {\n");
                  dfs (p->opr.op[2]);
                  printf ("}\n");
               }
               break;

            case PRINT:
               printf("cout << ");
               dfs (p->opr.op[0]);
               printf(" << endl;\n");
               break;

            case '=':

               printf ("%c = ", p->opr.op[0]->con_or_id.value_or_i + 'a');
               dfs (p->opr.op[1]);
               printf(";\n");
               break;

            case UMINUS:
               printf ("-");
               dfs (p->opr.op[0]);
               break;

            default:

               dfs (p->opr.op[0]);

               switch (p->opr.oper) {

                  case '+':
                     printf (" + ");
                     break;

                  case '-':
                     printf (" - ");
                     break;

                  case '*':
                     printf (" * ");
                     break;

                  case '/':
                     printf (" / ");
                     break;

                  case '^':
                     printf (" ^ ");
                     break;

                  case '<':
                     printf (" < ");
                     break;

                  case '>':
                     printf (" > ");
                     break;

                  case GE:
                     printf (" >= ");
                     break;

                  case LE:
                     printf (" <= ");
                     break;

                  case NE:
                     printf (" != ");
                     break;

                  case EQ:
                     printf (" == ");
                     break;
               }
               dfs (p->opr.op[1]);
         }
   }
}
